package com.example.zhengchaojun.arcgis.test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.esri.arcgisruntime.arcgisservices.LevelOfDetail;
import com.esri.arcgisruntime.arcgisservices.TileInfo;
import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.layers.ServiceImageTiledLayer;
import com.esri.arcgisruntime.mapping.ArcGISScene;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.SceneView;
import com.example.zhengchaojun.arcgis.R;
import com.example.zhengchaojun.arcgis.moremap.LayerInfoFactory;
import com.example.zhengchaojun.arcgis.moremap.MoreMapLayer;
import com.example.zhengchaojun.arcgis.moremap.MoreMapLayerInfo;
import com.example.zhengchaojun.arcgis.moremap.MoreMapLayerTypes;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 类描述：
 * 作者：郑朝军 on 2018/5/10
 * 邮箱：1250393285@qq.com
 * 公司：武汉智博创享科技有限公司
 * 修改人：郑朝军 on 2018/5/10
 * 修改备注：
 */
public class GaoDeActivity extends AppCompatActivity
{
    protected SceneView m_sceneView;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_solid);
        // access MapView from layout
        m_sceneView = findViewById(R.id.sceneView);
        Integer position = (Integer) getItem();
        MoreMapLayerInfo layerInfo = null;
        if (position == 0)
        {// 高德矢量图层（含路网，含注记）
            layerInfo = LayerInfoFactory.getLayerInfo(MoreMapLayerTypes.AMAP_VECTOR);
        }
        else if (position == 1)
        {// 高德影像图层（不含路网，不含注记）
            layerInfo = LayerInfoFactory.getLayerInfo(MoreMapLayerTypes.AMAP_IMAGE);
        }
        else if (position == 2)
        {//   百度矢量图层（含路网，含注记）
            layerInfo = LayerInfoFactory.getLayerInfo(MoreMapLayerTypes.BAIDU_MAP_VECTOR);
        }
        else if (position == 3)
        {// 百度影像图层（不含路网，不含注记）
            layerInfo = LayerInfoFactory.getLayerInfo(MoreMapLayerTypes.BAIDU_MAP_IMAGE);
        }
        else if (position == 4)
        {// 腾讯矢量图层（含路网，含注记）
            layerInfo = LayerInfoFactory.getLayerInfo(MoreMapLayerTypes.TENCENT_MAP_VECTOR);
        }
        else if (position == 5)
        {// 腾讯影像图层（不含路网，不含注记）
            layerInfo = LayerInfoFactory.getLayerInfo(MoreMapLayerTypes.TENCENT_MAP_IMAGE);
        }
        else if (position == 6)
        {// 天地图矢量图层（含路网，含注记）
            layerInfo = LayerInfoFactory.getLayerInfo(MoreMapLayerTypes.TIAN_VECTOR);
        }
        else
        {// 默认地图
            layerInfo = LayerInfoFactory.getLayerInfo(MoreMapLayerTypes.AMAP_VECTOR);
        }


        SpatialReference spatialReference = SpatialReference.create(layerInfo.getSrid());
        Envelope envelope = new Envelope(layerInfo.getxMin(), layerInfo.getyMin(), layerInfo.getxMax(), layerInfo.getyMax(), spatialReference);

        List<LevelOfDetail> mainLevelOfDetail = new ArrayList<>();
        for (int i = layerInfo.getMinZoomLevel(); i <= layerInfo.getMaxZoomLevel(); i++)
        {
            double[] scales = layerInfo.getScales();
            double[] resolutions = layerInfo.getResolutions();
            LevelOfDetail item = new LevelOfDetail(i, resolutions[i], scales[i]);
            mainLevelOfDetail.add(item);
        }
        int tileHeight = layerInfo.getTileWidth();
        int tileWidth = layerInfo.getTileHeight();

        Point ORIGIN_2000 = new Point(-20037508.342787, 20037508.342787, spatialReference);
        TileInfo tileInfo = new TileInfo(layerInfo.getDpi(), TileInfo.ImageFormat.PNG24, mainLevelOfDetail, ORIGIN_2000, spatialReference, tileHeight, tileWidth);

        ServiceImageTiledLayer webTiledLayer = new MoreMapLayer(layerInfo, tileInfo, envelope);
        Basemap tdtBasemap = new Basemap(webTiledLayer);
        ArcGISScene map = new ArcGISScene(tdtBasemap);
        m_sceneView.setScene(map);
        m_sceneView.setAttributionTextVisible(false);
    }

    /**
     * 获取上个界面返回过来的信息<br/>
     */
    public Serializable getItem()
    {
        return getIntent().getSerializableExtra("data");
    }
}